package org.itboys.mobile.service.mongo.site;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/22
 * 描述：雄安加盟网_
 */
@Service
public class SundaySiteAdvertiseMentService extends BaseMongoService<SundayAdvertisement> {
    @Autowired
    private ResourceHolder resourceHolder;
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Autowired
    private SundayProductCategoryService categoryService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayAdvertisement> getEntityClass() {
        return SundayAdvertisement.class;
    }

    /**
     * 根据广告位获取广告。不分页
     */
    public List<SundayAdvertisementDto> getAdvertisementByZone(String zoneKey){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("zoneKey",zoneKey);
        param.put("isDeleted",0);
        return pakageDtoMuilt(super.list(param));
    }

    public List<Map<String,Object>> getCategoryAdvertisement() {
        List<String> zoneKeys =new ArrayList<String>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", 0);
        param.put("parentId",0);
        List<SundayProductCategory> categories=categoryService.select(param);
        for (SundayProductCategory category :categories) {
            zoneKeys.add(category.getId()+"zoneKey_title");
        }
        List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
        if (ListUtil.isNull(zoneKeys)) return results;
        param.clear();
        param.put("zoneKey in", zoneKeys);
        param.put("isDeleted", 0);
        List<SundayAdvertisement> advertisements=super.list(param);
        for (SundayAdvertisement advertisement :advertisements) {
            Map<String,Object> result=new HashMap<String,Object>();
            result.put("categoryId",advertisement.getZoneKey().substring(0,3));
            result.put("name",advertisement.getName());
            results.add(result);
        }

        return results;
    }
    /**
     * 组装DTO
     * @param advertisement
     * @return
     */

    public SundayAdvertisementDto pakageDtoSingle(SundayAdvertisement advertisement){
        String url="/sunday/weixin/advertisement/html5?id="+advertisement.getId();
        String imgRoot=resourceHolder.getStringValue("imgRoot");
        String info="转跳信息说明。pageType为0，打开Url；pageType为1，打开商品详情，pageId为商品ID；pageType=2,打开商品列表，pageId为商品分类ID；pageType=3,打开商户详情（暂无），pageId为商户ID";
        return new SundayAdvertisementDto(
                advertisement.getId(),advertisement.getCreateTime(),advertisement.getCreateDate(),
                advertisement.getName(),advertisement.getViceName(),imgRoot+advertisement.getImage(), advertisement.getPageType(),
                advertisement.getPageId(),url,info);

    }

    public  List<SundayAdvertisementDto>  pakageDtoMuilt(List<SundayAdvertisement> advertisements){
        List<SundayAdvertisementDto> dtos = new ArrayList<SundayAdvertisementDto>();
        for(SundayAdvertisement advertisement:advertisements){
            dtos.add(pakageDtoSingle(advertisement));
        }
        return dtos;

    }
}
